import axios from 'axios'
import { Message } from 'element-ui'
import store from '@/store'
import SettingMer from "@/utils/settingMer";
const service = axios.create({
  baseURL: SettingMer.apiBaseURL,
  timeout: 60000, // 过期时间
  //headers:{"Content-Type":"application/x-www-form-urlencoded"},
  //withCredentials:true,
  responseType:'blob'
})

// request interceptor
service.interceptors.request.use(
  config => {
    // 发送请求之前做的
    const token = !store.getters.token?sessionStorage.getItem('token'):store.getters.token;
    if (token) {
      config.headers['Authori-zation'] = token
    }
    if(/get/i.test(config.method)){
      config.params = config.params || {}
      config.params.temp= Date.parse(new Date()) / 1000
    }
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

// response interceptor
service.interceptors.response.use(
  response => {
    return response.data;
  },
  error => {
    Message({
      message: '请求失败',
      type: 'error',
      duration: 5 * 1000
    })
    return Promise.reject(error)
  }
)

export default service

